package Client;

import java.io.*;

import javax.net.ssl.*;

import java.security.KeyStore;

/*
 * This example shows how to set up a key manager to do client
 * authentication if required by server.
 *
 * This program assumes that the client is not inside a firewall.
 * The application can be modified to connect to a server outside
 * the firewall by following SSLSocketClientWithTunneling.java.
 */
public class Client {
	static String host;
	static int port;
	static String path;
	static SSLSocket socket;
	static SSLSocketFactory factory;

	public static void main(String[] args) {
		Client client = null;
		try {

			client = new Client("localhost", 9999, "/testfolder/");
			System.out.println("1");
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		if (client != null) {
			client.run("doc1", "ghj58QA1");
		}else{
			System.out.println("Cannot create client. :(");
		}
	}

	public Client(String host, int port, String path) throws Exception {
		this.host = host;
		this.port = port;
		this.path = path;

	}

	public void run(String username, String password) {
		try {
			factory = null;

			/*
			 * Set up a key manager for client authentication if asked by the
			 * server. Use the implementation's default TrustStore and
			 * secureRandom routines.
			 */

			SSLContext ctx;
			KeyManagerFactory kmf;
			TrustManagerFactory tmf;
			KeyStore ks;
			try {

				char[] passphrase = password.toCharArray();

				ctx = SSLContext.getInstance("TLS");
				kmf = KeyManagerFactory.getInstance("SunX509");
				ks = KeyStore.getInstance("JKS");

				ks.load(new FileInputStream("cert/Clientside/" + username
						+ ".jks"), passphrase);

				kmf.init(ks, passphrase);

				passphrase = "59ddmaQi".toCharArray();

				tmf = TrustManagerFactory.getInstance("SunX509");
				ks = KeyStore.getInstance("JKS");
				ks.load(new FileInputStream("cert/Clientside/clnttrust.jks"),
						passphrase);
				tmf.init(ks);

				ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

				factory = ctx.getSocketFactory();
			} catch (Exception e) {
				throw new IOException(e.getMessage());
			}
			socket = (SSLSocket) factory.createSocket(host, port);
			System.out
					.println("Creating secure socket to " + host + ":" + port);

			socket.startHandshake();

			System.out.println("Just connected to "
					+ socket.getRemoteSocketAddress());

			sendText("new_list", "doc1", "nurse1", "a comment", "123456");
			sendText("new_pat", "doc1", "nurse1", "a comment", "123456");
			sendText("get_j", "doc1", "nurse1", "a comment", "123456");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void sendText(String data, String doctor, String nurse, String patientID,
			String comment) {
		try {
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())));

			if (out.checkError()) {
				System.out
						.println("SSLSocketClient: java.io.PrintWriter error");
			}
			InputStream inputstream = System.in;
			out.println();
			out.flush();
			InputStreamReader inputstreamreader = new InputStreamReader(
					inputstream);
			BufferedReader bufferedreader = new BufferedReader(
					inputstreamreader);

			OutputStream outputstream = socket.getOutputStream();
			OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
					outputstream);
			BufferedWriter in = new BufferedWriter(outputstreamwriter);

			String inputLine;
			inputLine = data + "#" + doctor + "#" + nurse + "#" + patientID
					+ "#" + comment + "#";
			in.write(inputLine);
			in.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String listen() {
		String returnString = "";
		try {
			InputStream inputstream = socket.getInputStream();
			InputStreamReader inputstreamreader = new InputStreamReader(
					inputstream);
			BufferedReader bufferedreader = new BufferedReader(
					inputstreamreader);

			String string = null;

			string = bufferedreader.readLine();
			System.out.println(string);
			returnString = returnString + string;


		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnString;
	}

	class MyHandshakeListener implements HandshakeCompletedListener {
		public void handshakeCompleted(HandshakeCompletedEvent e) {
			System.out.println("Handshake succesful!");
			System.out.println("Using cipher suite: " + e.getCipherSuite());
		}
	}
}